// 基于你的LiveingActivity的实际代码示例

class LiveingActivity : BaseActivity() {
    private lateinit var binding: ActivityLiveingBinding
    private lateinit var adapter: LiveRoomAdapter
    private var liveRooms: ArrayList<LiveEntity2> = arrayListOf()
    
    // 方法1：获取特定位置的TextView（基于你的代码）
    fun getTextViewFromPosition(position: Int): TextView? {
        val holder = binding.rv.findViewHolderForAdapterPosition(position)
        return if (holder is LiveRoomAdapter.LiveRoomViewHolder) {
            // 假设你的ViewHolder中有一个名为tvTitle的TextView
            holder.tvTitle // 你需要根据实际的ViewHolder结构调整
        } else {
            null
        }
    }
    
    // 方法2：获取当前可见item的TextView
    fun getCurrentVisibleTextView(): TextView? {
        val layoutManager = binding.rv.layoutManager as? LinearLayoutManager
        val position = layoutManager?.findFirstCompletelyVisibleItemPosition() ?: -1
        
        if (position != RecyclerView.NO_POSITION && position < liveRooms.size) {
            return getTextViewFromPosition(position)
        }
        return null
    }
    
    // 方法3：通过itemView的findViewById获取
    fun getTextViewByFindViewById(position: Int, textViewId: Int): TextView? {
        val holder = binding.rv.findViewHolderForAdapterPosition(position)
        return holder?.itemView?.findViewById(textViewId)
    }
    
    // 方法4：获取所有可见的TextView
    fun getAllVisibleTextViews(): List<TextView> {
        val visibleTextViews = mutableListOf<TextView>()
        val layoutManager = binding.rv.layoutManager as? LinearLayoutManager
        
        val firstVisible = layoutManager?.findFirstVisibleItemPosition() ?: -1
        val lastVisible = layoutManager?.findLastVisibleItemPosition() ?: -1
        
        for (position in firstVisible..lastVisible) {
            if (position >= 0 && position < liveRooms.size) {
                val textView = getTextViewFromPosition(position)
                textView?.let { visibleTextViews.add(it) }
            }
        }
        
        return visibleTextViews
    }
    
    // 使用示例
    fun exampleUsage() {
        // 获取第2个item的TextView并修改
        val textView = getTextViewFromPosition(2)
        textView?.let {
            it.text = "新的直播间标题"
            it.setTextColor(Color.RED)
        }
        
        // 获取当前可见item的TextView
        val currentTextView = getCurrentVisibleTextView()
        currentTextView?.let {
            it.text = "当前正在观看的直播间"
        }
        
        // 通过ID获取特定TextView
        val titleTextView = getTextViewByFindViewById(3, R.id.tv_title)
        titleTextView?.let {
            it.text = "通过ID获取的标题"
        }
        
        // 获取所有可见的TextView并批量修改
        val allTextViews = getAllVisibleTextViews()
        allTextViews.forEach { textView ->
            textView.setTextColor(Color.BLUE)
        }
    }
    
    // 在你的滚动监听中使用（基于你的代码）
    private fun setupScrollListener() {
        binding.rv.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
                super.onScrollStateChanged(recyclerView, newState)
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    val position = (recyclerView.layoutManager as LinearLayoutManager)
                        .findFirstCompletelyVisibleItemPosition()
                    
                    if (position != RecyclerView.NO_POSITION && position < liveRooms.size) {
                        // 获取当前可见item的TextView
                        val currentTextView = getTextViewFromPosition(position)
                        currentTextView?.let {
                            // 对当前可见的TextView进行操作
                            it.setTextColor(Color.GREEN)
                            Log.d("ScrollListener", "当前可见item的TextView: ${it.text}")
                        }
                        
                        // 你的原有逻辑
                        val newRoomId = liveRooms[position].room_id
                        val holder = binding.rv.findViewHolderForAdapterPosition(position)
                        if (holder is LiveRoomAdapter.LiveRoomViewHolder) {
                            adapter.onCurrentItemChanged(newRoomId, holder)
                        }
                    }
                }
            }
        })
    }
}

// 你的LiveRoomAdapter示例（需要根据实际情况调整）
class LiveRoomAdapter(private val dataList: List<LiveEntity2>) : 
    RecyclerView.Adapter<LiveRoomAdapter.LiveRoomViewHolder>() {
    
    class LiveRoomViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        // 根据你的实际布局文件调整这些ID
        val tvTitle: TextView = itemView.findViewById(R.id.tv_title)
        val tvViewerCount: TextView = itemView.findViewById(R.id.tv_viewer_count)
        val tvAnchorName: TextView = itemView.findViewById(R.id.tv_anchor_name)
        // 其他控件...
    }
    
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): LiveRoomViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_live_room, parent, false)
        return LiveRoomViewHolder(view)
    }
    
    override fun onBindViewHolder(holder: LiveRoomViewHolder, position: Int) {
        val item = dataList[position]
        holder.tvTitle.text = item.title
        holder.tvViewerCount.text = "${item.viewer_count}人观看"
        holder.tvAnchorName.text = item.anchor_name
        // 其他绑定逻辑...
    }
    
    override fun getItemCount(): Int = dataList.size
    
    // 添加获取特定位置TextView的方法
    fun getTextViewAtPosition(position: Int): TextView? {
        val holder = binding.rv.findViewHolderForAdapterPosition(position)
        return if (holder is LiveRoomViewHolder) {
            holder.tvTitle
        } else {
            null
        }
    }
} 